package com.bball.court.http;

import java.io.UnsupportedEncodingException;

import android.content.Context;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.bball.court.http.base.GsonPostRequest;
import com.bball.court.model.Court;
import com.google.gson.GsonBuilder;

public class PostCourtRequest extends GsonPostRequest<Court> {

    private String courtString;

    public PostCourtRequest(Context context, Court court, String url, String tag, final AddCourtRequestCallbacks callbacks) {
        super(context, url, Court.class, tag, new Listener<Court>() {
            @Override
            public void onResponse(Court court) {
                Log.i(getClass().getSimpleName(), "Court uploaded");
                callbacks.onAddCourtSuccess(court);
            }
        }, new ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(getClass().getSimpleName(), error.getMessage(), error);
                callbacks.onAddCourtFail(new RuntimeException(error));
            }
        });
        this.courtString = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().toJson(court);
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        try {
            return courtString.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            Log.e(getClass().getSimpleName(), e.getMessage(), e);
            return new byte[0];
        }
    }
    
    public String getBodyContentType() {
        return "application/json; charset=" + getParamsEncoding();
    }

}
